package com.bole.service.impl;

import com.bole.dto.car.BrowsingHistoryDto;
import com.bole.pojo.BrowsingHistoryDo;
import com.bole.service.BrowsingHistoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrowsingHistoryServiceImpl implements BrowsingHistoryService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<BrowsingHistoryDo> selectByUserId(Long userId) {
        List<BrowsingHistoryDo> historyDoList = mongoTemplate.find(new Query(Criteria.where("userId").is(String.valueOf(userId))), BrowsingHistoryDo.class);
        return historyDoList;
    }

    @Override
    public void insertHistory(Long userId, Long carId) {
        BrowsingHistoryDo browsingHistoryDo = new BrowsingHistoryDo();
        browsingHistoryDo.setUserId(Long.toString(userId));
        browsingHistoryDo.setCarId(carId);
        BrowsingHistoryDto browsingHistoryDto = new BrowsingHistoryDto();
        BeanUtils.copyProperties(browsingHistoryDo,browsingHistoryDto);
        mongoTemplate.insert(browsingHistoryDto);
    }
}
